package org.opens.scheduled;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.net.URLDecoder;

@Component
@Slf4j
public class PrintLogScheduled {

    /**
     * 每2秒打印一次日志
     */
    @Scheduled(cron ="*/2 * * * * ?")
    public void printLog() throws Exception {
        log.debug("debug level log.");
        log.info("info level log.");
        log.warn("warn level log.");
        log.error("error level log.");

        //第一种
        File path = new File(ResourceUtils.getURL("classpath:").getPath());
        if(!path.exists()) path = new File("");
        log.info("第1种: " + path.getAbsolutePath());
        //第二种
        log.info("第2种: " + System.getProperty("user.dir"));
        //第三种
        String path1 = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        log.info("第3种: " + URLDecoder.decode(path1, "utf-8"));
        //第四种
        String path2 = ResourceUtils.getURL("classpath:").getPath();
        log.info("第4种: " + path2);
        //第五种, 可以获取到jar所在的路径, 可以在stater中写一个controller下载日志文件
        ApplicationHome h = new ApplicationHome(getClass());
        File jarF = h.getSource();
        log.info("第5种: " + jarF.getParentFile().toString());
    }

}
